<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('商品管理列表')" />
    <link th:href="@{/ajax/libs/jquery-layout/jquery.layout-latest.css}" rel="stylesheet"/>
</head>
<body class="gray-bg">
     <div class="container-div" id="resizable">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>商品名称：</label>
                                <input type="text" name="goodsName"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
     <script th:src="@{/js/jquery-ui-1.10.4.min.js}"></script>
     <script th:src="@{/ajax/libs/jquery-layout/jquery.layout-latest.js}"></script>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('lucky:goods:edit')}]];
        var removeFlag = [[${@permission.hasPermi('lucky:goods:remove')}]];
        var prefix = ctx + "lucky/goods";
        var firstAsyncSuccessFlag = 0;
        var id = '';
        var overAllIds = new Array();             // 全局保存选中行的对象
        var allIds =  [[${selectIds}]]
        $(function() {
            //加载已选中的
            for(i in allIds){
                overAllIds.push(allIds[i]);
            }
            // $('body').layout({ west__size: 285 });
            $("#resizable").resizable();
            queryGoodsList();
        });
        function queryGoodsList() {
                var options = {
                    url: prefix + "/list",
                    createUrl: prefix + "/add",
                    updateUrl: prefix + "/edit/{id}",
                    removeUrl: prefix + "/remove",
                    exportUrl: prefix + "/export",
                    modalName: "商品管理",
                    columns: [{
                            checkbox: true,
                            formatter: function (i,row) {            // 每次加载 checkbox 时判断当前 row 的 id 是否已经存在全局 Set() 里
                                if($.inArray(row.goodsId,overAllIds)!=-1){  // 因为 判断数组里有没有这个 id
                                    return {
                                        checked : true               // 存在则选中
                                    }
                                }
                            }
                        },
                        {
                            field: 'goodsId',
                            title: '商品ID',
                            visible: false
                        },
                        {
                            field: 'goodsName',
                            title: '商品名称'
                        },
                        {
                            field: 'goodsPic',
                            title: '商品图片',
                            formatter: function (value, row, index) {
                                return '<img src="' + value + '" width="80" height="50">';
                            }
                        },
                        {
                            field: 'goodsfee',
                            title: '商品价格'
                        },
                        {
                            field: 'chance',
                            title: '开出概率'
                        },
                        {
                            field: 'stockcount',
                            title: '库存数'
                        },
                        {
                            field: 'exchargefee',
                            title: '商品兑换价格'
                        },
                        {
                            field: 'remark',
                            title: '备注'
                        },
                    ]
                };

            $.table.init(options);
            //选中事件
            $('#bootstrap-table').on('uncheck.bs.table check.bs.table check-all.bs.table uncheck-all.bs.table',function(e,rows){
                var datas = $.isArray(rows) ? rows : [rows];        // 点击时获取选中的行或取消选中的行
                examine(e.type,datas);                                 // 保存到全局 Set() 里
            });
        }
        function examine(type,datas){
            if(type.indexOf('uncheck')==-1){
                $.each(datas,function(i,v){
                    // 添加时，判断一行或多行的 id 是否已经在数组里 不存则添加　
                    overAllIds.indexOf(v.goodsId) == -1 ? overAllIds.push(v.goodsId) : -1;
                });
            }else{
                $.each(datas,function(i,v){
                    overAllIds.splice(overAllIds.indexOf(v.goodsId),1);    //删除取消选中行
                });
            }
        }
        /* 添加商品-选择商品-提交 */
        function submitHandler() {
            var blindboxId = [[${blindboxId}]]
            var rows = $.table.selectFirstColumns();
            if (overAllIds.length <= 0 ) {
                return;
            }
            $.ajax({
                cache : true,
                type : "POST",
                url :  prefix + "/addGoodsByBox",
                data : {
                    "idList": overAllIds.join(),
                    "blindboxId": blindboxId
                },
                async : false,
                error : function(request) {
                    $.modal.alertError("系统错误");
                },
                success : function(data) {
                    $.modal.close();
                    if(data.indexOf("成功") != -1) {
                        parent.$.modal.alertSuccess(data);
                    }else{
                        parent.$.modal.alertError(data);
                    }
                    parent.refresh();
                }
            });
        }
    </script>
</body>
</html>